package cn.singno.bob.web.utils;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

/**
 * spring 容器工具类
 * @author bob
 *
 */
public class SpringContextHelper implements DisposableBean, ApplicationContextAware{

	private static ApplicationContext applicationContext = null;
	
	public void setApplicationContext(ApplicationContext applicationContext)
			throws BeansException {
		SpringContextHelper.applicationContext = applicationContext;
	}

	public void destroy() throws Exception {
		applicationContext = null;
		
	}

	@SuppressWarnings("unchecked")
	public static <T> T getBean(Class<?> clazz){
		return (T) applicationContext.getBean(clazz);
	}
	
	@SuppressWarnings("unchecked")
	public static <T> T getBean(String beanName){
		return (T) applicationContext.getBean(beanName);
	}
	
	
}
